package com.libre.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.Result;
import com.sun.management.OperatingSystemMXBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.management.ManagementFactory;

@Slf4j
@RestController
@RequestMapping("/server2")
public class ServerMonitoring2Controller {

    @RequestMapping("/info")
    public Result<JSONObject> main() {
        JSONObject data = new JSONObject();
        JSONObject osInfo = getOSInfo();
        data.put("os", osInfo);
        //获取内存
        JSONObject memInfo = getMemInfo();
        data.put("mem", memInfo);
        //获取cpu
        JSONObject cpuInfo = getCpuInfo();
        data.put("cpu", cpuInfo);
        //获取磁盘
        JSONArray diskInfo = getDiskInfo();
        data.put("disk", diskInfo);
        //获取磁盘
        JSONObject dountDisk = getMountDisk(File.separator + "home");
        data.put("dountDisk", dountDisk);
        return new ResultUtil<JSONObject>().setData(data);
    }

    //操作系统
    public static JSONObject getOSInfo() {
        JSONObject obj = new JSONObject();
        // 虚拟机级内存情况查询
        int byteToMb = 1024 * 1024;
        Runtime rt = Runtime.getRuntime();
        long vmTotal = rt.totalMemory() / byteToMb;
        long vmFree = rt.freeMemory() / byteToMb;
        long vmMax = rt.maxMemory() / byteToMb;
        long vmUse = vmTotal - vmFree;
//        log.info("JVM内存已用的空间为：" + vmUse + " MB");
        obj.put("vmUse", vmUse);
//        log.info("JVM内存的空闲空间为：" + vmFree + " MB");
        obj.put("vmFree", vmFree);
//        log.info("JVM总内存空间为：" + vmTotal + " MB");
        obj.put("vmTotal", vmTotal);
//        log.info("JVM最大内存空间为：" + vmMax + " MB");
        obj.put("vmMax", vmMax);
        // 操作系统级内存情况查询
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        String os = System.getProperty("os.name");
//        log.info("操作系统：" + os);
        long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
        long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
        long physicalUse = physicalTotal - physicalFree;

//        log.info("操作系统的版本：" + os);
        obj.put("os", os);
//        log.info("操作系统物理内存已用的空间为：" + physicalFree + " MB");
        obj.put("physicalFree", physicalFree + " MB");
//        log.info("操作系统物理内存的空闲空间为：" + physicalUse + " MB");
        obj.put("physicalUse", physicalUse + " MB");
//        log.info("操作系统总物理内存：" + physicalTotal + " MB");
        obj.put("physicalTotal", physicalTotal + " MB");

        // 获得线程总数
        ThreadGroup parentThread;
        int totalThread = 0;
        for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent()) {
            totalThread = parentThread.activeCount();
        }
//        log.info("获得线程总数:" + totalThread);
        obj.put("totalThread", totalThread);
        return obj;
    }

    //磁盘
    public static JSONArray getDiskInfo() {
        JSONArray arr = new JSONArray();
        File[] disks = File.listRoots();
        for (File file : disks) {
            JSONObject obj = new JSONObject();
            // 磁盘
            String path = file.getPath();
            obj.put("path", path);
            //剩余存储空间
            String freeSpace = file.getFreeSpace() / 1024 / 1024 / 1024 + "G";
            obj.put("freeSpace", freeSpace);
            //实际可用空间
            String usableSpace = file.getUsableSpace() / 1024 / 1024 / 1024 + "G";
            obj.put("usableSpace", usableSpace);
            //总容量
            String totalSpace = file.getTotalSpace() / 1024 / 1024 / 1024 + "G";
            obj.put("totalSpace", totalSpace);
            arr.add(obj);
        }
        return arr;
    }

    // 挂载磁盘（如关在更目录下：/,挂在home目录下：/home）
    public static JSONObject getMountDisk(String filePath) {
        File file = new File(filePath);
        JSONObject obj = new JSONObject();
        // 磁盘
        String path = file.getPath();
        obj.put("path", path);
        //剩余存储空间
        String freeSpace = file.getFreeSpace() / 1024 / 1024 / 1024 + "G";
        obj.put("freeSpace", freeSpace);
        //实际可用空间
        String usableSpace = file.getUsableSpace() / 1024 / 1024 / 1024 + "G";
        obj.put("usableSpace", usableSpace);
        //总容量
        String totalSpace = file.getTotalSpace() / 1024 / 1024 / 1024 + "G";
        obj.put("totalSpace", totalSpace);
        return obj;
    }

    //内存
    public static JSONObject getMemInfo() {
        JSONObject obj = new JSONObject();
        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        //总内存
        // log.info("Total RAM：" + mem.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024 + "GB");
        String totalRAM = mem.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024 + "GB";
        obj.put("totalRAM", totalRAM);
        //可用内存
        // log.info("Available　RAM：" + mem.getFreePhysicalMemorySize() / 1024 / 1024 / 1024 + "GB");
        String availableRAM = mem.getFreePhysicalMemorySize() / 1024 / 1024 / 1024 + "GB";
        obj.put("availableRAM", availableRAM);
        return obj;
    }

    //Cpu
    public static JSONObject getCpuInfo() {
        JSONObject obj = new JSONObject();
        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double processCpuLoad = mem.getProcessCpuLoad();//过程CPU负载
        double systemCpuLoad = mem.getSystemCpuLoad();//系统CPU负载
        long processCpuTime = mem.getProcessCpuTime();//过程CPU时间
        /* System.out.println("cpu1:"+processCpuLoad);
        System.out.println("cpu2:"+systemCpuLoad);
        System.out.println("cpu3:"+processCpuTime);*/
        obj.put("processCpuLoad", processCpuLoad);
        obj.put("systemCpuLoad", systemCpuLoad);
        obj.put("processCpuTime", processCpuTime);
        return obj;
    }

}
